<template>
  <div>
    <h1>类型管理</h1>
    <el-button type="parmary" @click="AddRootType()">新增类型</el-button>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="id" label="Id" />
      <el-table-column prop="name" label="类型名称" />
      <el-table-column prop="typeStatus" label="状态" />
      <el-table-column prop="typeTop" label="排序" />
      <el-table-column prop="typeImage" label="图片">
        <template #default="scope">
          <img
            v-if="scope.row.typeImage != null"
            style="width: 100px; height: 100px"
            :src="scope.row.typeImage"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="parmary" size="small" @click="onAddType(scope.row)"
            >新增</el-button
          >
          <el-button type="success" size="small" @click="onEditType(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="onDeleteType(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-dialog v-model="dialogFormVisible" title="新增商品类型" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="分类名称"
            :label-width="formLabelWidth"
            prop="typeName"
          >
            <el-input v-model="form.typeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="分类图片"
            :label-width="formLabelWidth"
            prop="typeImage"
          >
            <!-- <el-input v-model="form.typeImage" autocomplete="off" /> -->
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/user/type/uploadTypeImage"
              :show-file-list="false"
              :headers="headers"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="typeTop"
          >
            <el-input v-model="form.typeTop" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="typeStatus"
          >
            <el-radio-group v-model="form.typeStatus">
              <el-radio :value="0">显示</el-radio>
              <el-radio :value="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              确认
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getTypeTreeService, addTypeService } from "../api/api";
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
import type { FormInstance, FormRules } from "element-plus";
const ruleFormRef = ref<FormInstance>();

//校验图片非空
const checkImage = (rule: any, value: any, callback: any) => {
  if (imageUrl.value == null || imageUrl.value == "") {
    return callback(new Error("请上传图片"));
  } else {
    return callback();
  }
};
//校验规则
const rules = ref<FormRules>({
  typeName: [{ required: true, message: "请填写分类名称", trigger: "blur" }],
  typeTop: [{ required: true, message: "请填写排序", trigger: "blur" }],
  typeStatus: [{ required: true, message: "请选择状态", trigger: "change" }],
  typeImage: [{ required: true, validator: checkImage, trigger: "blur" }],
});
//校验提交事件
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      onSubmitAddType();
    } else {
      console.log("error submit!", fields);
    }
  });
};

import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";

const imageUrl = ref("");
const headers = ref({
  token: sessionStorage.getItem("token"),
});
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
};

const form = ref({
  typeId: null,
  typeImage: null,
  typeName: null,
  typePid: null,
  typeStatus: null,
  typeTop: null,
});
const tableData = ref([]);
const getTypeTree = () => {
  getTypeTreeService().then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
    }
  });
};

//新增顶级类型
const AddRootType = () => {
  imageUrl.value = "";
  form.value = {
    typeId: null,
    typeImage: null,
    typeName: null,
    typePid: 0,
    typeStatus: null,
    typeTop: null,
  };
  dialogFormVisible.value = true;
};

//新增子类型
//打开弹框
const onAddType = (row: any) => {
  dialogFormVisible.value = true;
  imageUrl.value = "";
  form.value = {
    typeId: null,
    typeImage: null,
    typeName: null,
    typePid: row.id,
    typeStatus: null,
    typeTop: null,
  };
};
//添加
const onSubmitAddType = () => {
  form.value.typeImage = imageUrl;

  addTypeService(form.value).then((res) => {
    if (res.data.code == 200) {
      dialogFormVisible.value = false;
      ElMessage.success("添加成功");
      getTypeTree();
    } else {
      ElMessage.error("添加失败");
    }
  });
};
//编辑
const onEditType = (row: any) => {};
//删除
const onDeleteType = (row: any) => {};

onMounted(() => {
  getTypeTree();
});
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
